package portfelo.control;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.DataSource;

import portfelo.db.QueryDB;
import portfelo.dto.AccountDto;
import portfelo.dto.PhotoDto;
import portfelo.dto.TagDto;

public class Control extends HttpServlet {
	//query with the queryDB
		private QueryDB query = null;
		
		//boolean for if tag was changes
		//if so, sets the attributes for a tag change
		private boolean tagChange;
		
		//boolean for if image was changed
		//if so, sets the attributes for an image change
		private boolean imageChange;
		
		public String getServletInfo()
		{
       return "this does stuff";
		}
		
		public void init(ServletConfig config)
		{
			query = new QueryDB();
			
			//first time we run, we should be displaying the images
			tagChange = true;
			imageChange = true;
		}
		
		public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
		{
			if(query == null)
				query = new QueryDB();
			
			String action = request.getParameter("action");
			PhotoDto image = null;
			if(action != null)
			{
				if(action.equals("up") || action.equals("down"))
					tagAction(action, null);
				else if(action.equals("settag"))
					tagAction(action, request.getParameter("selectedtag"));
				else if(action.equals("left") || action.equals("right"))
					image = photoAction(action);
			}
			
//			private List<TagDto> tags; //list of tags for the current image
//			private PhotoDto currentImage; //currently selected image
//			private TagDto currentTag; //currently selected tag
//			private List<PhotoDto> tagImages; //List of images with the current tag
			
//			request.setAttribute("name", "value");
//			request.getRequestDispatcher("page.jsp").forward(request, response);

//			request.setAttribute("currentTag", currentTag);
//			request.setAttribute("tagList", tagList);
//			request.setAttribute("currentImage", currentImage);
//			request.setAttribute("imageList", imageList);
			
			List<TagDto> tags = query.getTags();
			PhotoDto currentImage = query.getCurrentImage();
			TagDto currentTag = query.getCurrentTag();
			List<PhotoDto> tagImages = query.getCurrentTagImages();
			
			TagDto tagList[] = new TagDto[tags.size()];
			for(int i = 0; i < tags.size(); i++)
			{
				tagList[i] = tags.get(i);
			}
			String imageList[] = new String[tagImages.size()];
			for(int i = 0; i < tagImages.size(); i++)
			{
				imageList[i] = tagImages.get(i).getFullyQualifiedName();
			}
			
			//for image changes
			//should change the displayed image and image info
			//change the list of image tags
			
			//list of strings of the tags for current image
			String currentTags[] = new String[currentImage.getPhotoTagList().size()];
			for(int i = 0; i < currentTags.length; i++)
			{
				currentTags[i] = currentImage.getPhotoTagList().get(i).getRole();
				currentTags[i] += ": " + currentImage.getPhotoTagList().get(i).getAccountDto().getFirstName();
				currentTags[i] += " " + currentImage.getPhotoTagList().get(i).getAccountDto().getLastName();
				
			}
			
			request.setAttribute("imageUrl", currentImage.getFullyQualifiedName());
			request.setAttribute("imageId", currentImage.getPhotoId());
			request.setAttribute("imageName", currentImage.getTitle());
			request.setAttribute("imageUser", currentImage.getAccountId());		
			request.setAttribute("imageLocation", currentImage.getLocation());
			
			//date will be null
			request.setAttribute("imageDateTaken", currentImage.getDateTaken());
			request.setAttribute("imageTags", currentTags);
				
			request.setAttribute("taggedImagesUrl", imageList);
			
			
			
			//for tag changes
			//should change current displayed tag
			AccountDto acc = currentTag.getAccountDto();
			request.setAttribute("tagName", acc.getFirstName() + " " + acc.getLastName());
				
			//no plans for an avatar, will be null
			request.setAttribute("tagAvatar", acc.getAvatar());
			
			request.setAttribute("tagRole", currentTag.getRole());
			//request.setAttribute("currentTag", currentTag);

			
			
			//
			
			tagChange = false;
			imageChange = false;
			request.getRequestDispatcher("portfelowithimagelist.jsp").forward(request, response);
		}
		
		public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
		{
			doGet(request,response);
		}
		
		//maybe not necessary
		public void destroy()
		{
			query.close();
		}
		
		private void tagAction(String action, String setTag)
		{
			tagChange = true;
			if(action.equals("up"))
			{
				query.setNextTag();
			}
			else if(action.equals("down"))
			{
				query.setPreviousTag();
			}
			else if(action.equals("settag"))
			{
				query.setTag(setTag);
			}
		}
		
		private PhotoDto photoAction(String action)
		{
			imageChange = true;
			PhotoDto ret = null;
			if(action.equals("left"))
				ret = query.getPreviousImage();
			else if(action.equals("right"))
				ret = query.getNextImage();
			
			return ret;
		}
	

}
